<?php
/**
 * Created by PhpStorm.
 * User: luzhaoyang
 * Date: 2017/10/13
 * Time: 12:19
 */

class Wenjuan extends Base_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('wenjuan_model');
    }

    public function getWenjuanQuestion()
    {
        $get=$this->input->get();
        if (!isset($get['wenjuanid'])||!is_numeric($get['wenjuanid'])) $this->error();
        $res = $this->wenjuan_model->getWenjuanQuestion($get['wenjuanid']);
        $this->success($res);
    }

    public function checkUserComplish()
    {
        if(!isset($_SESSION)) session_start();
        if (isset($_SESSION['uid'])){
            $wenjuanid=$this->input->get('wenjuanid');
            if (!isset($wenjuanid)||!is_numeric($wenjuanid)) $this->error();
            //判定用户是否已经填写过问卷
            $iscomplished=$this->wenjuan_model->checkUserComplish($wenjuanid,$_SESSION['uid']);
            $this->success($iscomplished);
        }else{$this->error();}
    }

    public function submitAns()
    {
        $post=$this->input->post();
        if (!is_numeric($post['uid'])||!is_numeric($post['wenjuanid'])||empty($post['ans'])) $this->error();
        $uid=(int)$post['uid'];
        $wenjuanid=(int)$post['wenjuanid'];
        $ans=$post['ans'];

        $data=$this->wenjuan_model->submitAns($uid,$wenjuanid,$ans);
        $this->success($data);
    }

    public function submitName()
    {
        $post=$this->input->post();
        if (!is_numeric($post['uid'])||!is_numeric($post['wenjuanid'])||empty($post['name'])) $this->error();
        $uid=(int)$post['uid'];
        $wenjuanid=(int)$post['wenjuanid'];
        $name=$post['name'];
        $this->wenjuan_model->submitName($uid,$name,$wenjuanid);
        $this->success();
    }

    public function invalid()
    {
        echo '<p style="font-size: 30px;margin: 44px auto;">问卷已经失效！</p>';
        exit();
    }

    public function getStatis($wenjuanid)
    {
        $res=$this->wenjuan_model->getStatis($wenjuanid);
//        echo '<pre>';
//        var_dump($res);
//        echo '</pre>';
        $this->load->view('wjresult',array('results'=>$res));
    }

    public function test()
    {
//        $ans = array(
//            array('key'=>'a','qid'=>56),array('key'=>'a','qid'=>57),array('key'=>'a','qid'=>58),array('key'=>'a','qid'=>59),array('key'=>'a','qid'=>60),
//            array('key'=>'a','qid'=>61),array('key'=>'b','qid'=>62),array('key'=>'b','qid'=>63),array('key'=>'b','qid'=>64),array('key'=>'b','qid'=>65),
//            array('key'=>'b','qid'=>66),array('key'=>'b','qid'=>67),array('key'=>'a','qid'=>68),array('key'=>'a','qid'=>69),array('key'=>'a','qid'=>70),
//            array('key'=>'a','qid'=>71),array('key'=>'b','qid'=>72),array('key'=>'b','qid'=>73),array('key'=>'b','qid'=>74),array('key'=>'a','qid'=>75),
//            array('key'=>'a','qid'=>76),array('key'=>'b','qid'=>77),array('key'=>'a','qid'=>78),array('key'=>'a','qid'=>79),array('key'=>'b','qid'=>80),
//            array('key'=>'b','qid'=>81),array('key'=>'a','qid'=>82),array('key'=>'b','qid'=>83),array('key'=>'b','qid'=>84),array('key'=>'b','qid'=>85),
//            array('key'=>'a','qid'=>86),array('key'=>'a','qid'=>87),array('key'=>'a','qid'=>88),array('key'=>'a','qid'=>89),array('key'=>'b','qid'=>90),
//            array('key'=>'b','qid'=>91),array('key'=>'b','qid'=>92),array('key'=>'b','qid'=>93),array('key'=>'b','qid'=>94),array('key'=>'a','qid'=>95),
//            array('key'=>'a','qid'=>96),array('key'=>'a','qid'=>97),array('key'=>'b','qid'=>98),array('key'=>'a','qid'=>99),array('key'=>'a','qid'=>100),
//            array('key'=>'b','qid'=>101),array('key'=>'a','qid'=>102),array('key'=>'a','qid'=>103)
//        );
//        $res = $this->wenjuan_model->submitAns(3,5,$ans);
//        echo '<pre>';
//        var_dump($res);
//        echo '</pre>';
        $data = $this->wenjuan_model->checkUserComplish(5,3);
        echo '<pre>';
        var_dump($data);
        echo '</pre>';
        exit();
    }

}